from rest_framework import serializers

from goods.models import SKU, SKUSpecification, GoodsCategory
from goods.models import SPU, SPUSpecification, SpecificationOption


class SpecsSerializer(serializers.ModelSerializer):
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()
    class Meta:
        model = SKUSpecification
        fields = ("spec_id","option_id")


class SKUSerializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()
    category = serializers.StringRelatedField()
    category_id = serializers.IntegerField()

    specs = SpecsSerializer(many=True)

    class Meta:
        model = SKU
        exclude = ("default_image","comments","update_time","create_time")

    def create(self, validated_data):
        specs = validated_data.pop("specs")
        instance = super().create(validated_data)
        for spec in specs:
            spec["sku_id"] = instance.id
            SKUSpecification.objects.create(**spec)

        return instance

    def update(self, instance, validated_data):
        specs = validated_data.pop("specs")
        instance = super().update(instance,validated_data)
        SKUSpecification.objects.filter(sku=instance).delete()
        for spec in specs:
            spec["sku_id"] = instance.id
            SKUSpecification.objects.create(**spec)
        return instance


# [
#         {
#             "id": "商品分类id",
#             "name": "商品分类名称"
#         },
#         ...
#     ]

# "counts": "商品SPU总数量",
#         "lists": [
#             {
#                 "id": "商品SKU ID",
#                 "name": "商品SKU名称",
#                 "spu": "商品SPU名称",
#                 "spu_id": "商品SPU ID",
#                 "caption": "商品副标题",
#                 "category_id": "三级分类id",
#                 "category": "三级分类名称",
#                 "price": "价格",
#                 "cost_price": "进价",
#                 "market_price": "市场价格",
#                 "stock": "库存",
#                 "sales": "销量",
#                 "is_launched": "上下架",
#                 "specs": [
#                     {
#                         "spec_id": "规格id",
#                         "option_id": "选项id"
#                     },
#                     ...
#                 ]
#             },
#             ...
#           ],
#             "page": "页码",
#             "pages": "总页数",
#             "pagesize": "页容量"
#       }


class CategoriesSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()

    class Meta:
        model = GoodsCategory
        fields = ("id","name")


class SPUSimpleSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()

    class Meta:
        model = SPU
        fields = ("id","name")


class SpecificationOptionSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()

    class Meta:
        model = SpecificationOption
        fields = ("id","value")


class SPUSepecificationSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField()
    spu_id = serializers.IntegerField()
    spu = serializers.StringRelatedField()

    options = SpecificationOptionSerializer(many=True)

    class Meta:
        model = SPUSpecification
        fields = ("id","name","spu","spu_id","options")

